﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.templates;
using l1cs.server.datatables;
using l1cs.server.serverpackets;

namespace l1cs.server.model.Instance
{
    public class L1HousekeeperInstance : L1NpcInstance
    {
        private static Logger _log = Logger.getLogger( typeof( L1HousekeeperInstance ).FullName );

        public L1HousekeeperInstance( L1Npc template ) : base( template ) { }

        override public void onAction( L1PcInstance pc )
        {
            L1Attack attack = new L1Attack( pc, this );
            attack.calcHit();
            attack.action();
        }

        override public void onTalkAction( L1PcInstance pc )
        {
            int objid = getId();
            L1NpcTalkData talking = NPCTalkDataTable.getInstance().getTemplate( getNpcTemplate().get_npcId() );
            int npcid = getNpcTemplate().get_npcId();
            String htmlid = null;
            String[] htmldata = null;
            bool isOwner = false;

            if ( talking != null )
            {
                // 話しかけたPCが所有者とそのクラン員かどうか調べる
                L1Clan clan = L1World.getInstance().getClan( pc.getClanname() );
                if ( clan != null )
                {
                    int houseId = clan.getHouseId();
                    if ( houseId != 0 )
                    {
                        L1House house = HouseTable.getInstance()
                                .getHouseTable( houseId );
                        if ( npcid == house.getKeeperId() )
                        {
                            isOwner = true;
                        }
                    }
                }

                // 所有者とそのクラン員以外なら会話内容を変える
                if ( !isOwner )
                {
                    // Housekeeperが属するアジトを取得する
                    L1House targetHouse = null;
                    foreach ( L1House house in HouseTable.getInstance().getHouseTableList() )
                    {
                        if ( npcid == house.getKeeperId() )
                        {
                            targetHouse = house;
                            break;
                        }
                    }

                    // アジトがに所有者が居るかどうか調べる
                    bool isOccupy = false;
                    String clanName = null;
                    String leaderName = null;
                    foreach ( L1Clan targetClan in L1World.getInstance().getAllClans() )
                    {
                        if ( targetHouse.getHouseId() == targetClan.getHouseId() )
                        {
                            isOccupy = true;
                            clanName = targetClan.getClanName();
                            leaderName = targetClan.getLeaderName();
                            break;
                        }
                    }

                    // 会話内容を設定する
                    if ( isOccupy )
                    { // 所有者あり
                        htmlid = "agname";
                        htmldata = new String[] { clanName, leaderName,
                                targetHouse.getHouseName() };
                    }
                    else
                    { // 所有者なし(競売中)
                        htmlid = "agnoname";
                        htmldata = new String[] { targetHouse.getHouseName() };
                    }
                }

                // html表示パケット送信
                if ( htmlid != null )
                { // htmlidが指定されている場合
                    if ( htmldata != null )
                    { // html指定がある場合は表示
                        pc.sendPackets( new S_NPCTalkReturn( objid, htmlid, htmldata ) );
                    }
                    else
                    {
                        pc.sendPackets( new S_NPCTalkReturn( objid, htmlid ) );
                    }
                }
                else
                {
                    if ( pc.getLawful() < -1000 )
                    { // プレイヤーがカオティック
                        pc.sendPackets( new S_NPCTalkReturn( talking, objid, 2 ) );
                    }
                    else
                    {
                        pc.sendPackets( new S_NPCTalkReturn( talking, objid, 1 ) );
                    }
                }
            }
        }

        override public void onFinalAction( L1PcInstance pc, String action ) { }

        public void doFinalAction( L1PcInstance pc ) { }
    }
}
